import logging
import unittest

import requests

from api.approve_api import ApproveApi
from api.login_api import LoginApi
from api.recharge_api import RechargeApi
from api.register_api import RegisterAPI
from api.tender_api import TenderAPI
from config import *
from utils import read_html, common_assert, clear_member_data


class TestTenderList(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        clear_member_data("18500000008")

    def setUp(self):
        self.session = requests.Session()
        self.register = RegisterAPI(self.session)
        self.login = LoginApi(self.session)
        self.approve = ApproveApi(self.session)
        self.recharge = RechargeApi(self.session)
        self.tender = TenderAPI(self.session)

    def tearDown(self):
        self.session.close()

    def test_tender_list(self):
        try:
            # 注册图片验证码接口
            resp = self.register.captcha_code_api(RANDINT)
            self.assertEqual(200, resp.status_code)
            # 注册短信验证码接口
            resp = self.register.phone_code_api(PHONE, CAPTCHA)
            common_assert(self, resp, {"expect_code": 200, "expect_status": 200, "expect_description": "短信发送成功"})
            # 注册接口
            resp = self.register.register_api(PHONE, PASSWORD, CAPTCHA, PHONE_CODE, INVITE_PHONE)
            common_assert(self, resp, {"expect_code": 200, "expect_status": 200, "expect_description": "注册成功"})
            # 登录接口
            resp = self.login.login_api(PHONE, PASSWORD)
            common_assert(self, resp, {"expect_code": 200, "expect_status": 200, "expect_description": "登录成功"})
            # 实名认证接口
            resp = self.approve.approve_api(NAME, CARD_ID)
            common_assert(self, resp, {"expect_code": 200, "expect_status": 200, "expect_description": "提交成功"})
            # 后台开户接口
            resp = self.approve.trust_api()
            common_assert(self, resp, {"expect_code": 200, "expect_status": 200, "expect_description": "form"})
            result = read_html(resp.json()["description"]["form"], "html.parser", "input")
            # 三方开户接口
            resp = self.approve.tripartite_trust_api(result[0], result[1])
            self.assertIn("OK", resp.text)
            # 充值图片验证码接口
            resp= self.recharge.captcha_code_api(RANDINT)
            self.assertEqual(200, resp.status_code)
            # 后台充值接口
            resp = self.recharge.recharge_api(RECHARGE_AMOUNT, CAPTCHA)
            common_assert(self, resp, {"expect_code": 200, "expect_status": 200, "expect_description": "form"})
            result = read_html(resp.json()["description"]["form"], "html.parser", "input")
            # 三方充值接口
            resp = self.recharge.tripartite_recharge_api(result[0], result[1])
            self.assertIn("OK", resp.text)
            # 后台投资接口
            resp = self.tender.tender_api(LOAN_ID, TENDER_AMOUNT, PASSWORD)
            common_assert(self, resp, {"expect_code": 200, "expect_status": 200, "expect_description": "form"})
            # 三方投资接口
            resp = self.tender.tripartite_tender_api(result[0], result[1])
            self.assertIn("OK", resp.text)
        except Exception as e:
            logging.error(f"断言失败: {e}")
            raise
